home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap7 / 7_3 / ht73.txt < prev    next >
Encoding:
Text File  |  1996-06-15  |  2.7 KB  |  118 lines

  1. #!/usr2/local/bin/perl -w
  2.  
  3. use SDBM_File;
  4. use Fcntl;
  5. use POSIX;
  6.  
  7. require "cgilib.pl";
  8.  
  9.  
  10. sub calcTotals
  11. {
  12.     my $votes = 0;
  13.     my $sum   = 0;
  14.     my $i     = 1;
  15.  
  16.     foreach (@_)
  17.     {
  18.         $sum   += $_*$i++;
  19.         $votes += $_;
  20.     }
  21.  
  22.     return ($sum/$votes,$votes);
  23. }
  24.  
  25. readParse(*dict);
  26.  
  27. print <<EOH;
  28. Content-type: text/html
  29.  
  30. <HTML>
  31. <HEAD><TITLE>Waite Movie Reviews</TITLE></HEAD>
  32. <BODY>
  33.  
  34. <H1>Welcome to Waite Movie Reviews</H1>
  35. <HR>
  36. EOH
  37.  
  38. # Display the dbm record for each movie, sorted by movie title
  39. #
  40. # Data is stored in the following format:
  41. #    Field    Desc
  42. #        0    last score given
  43. #        1    # of scores of 1
  44. #        2    # of scores of 2
  45. #        . . .
  46. #        10   # of scores of 10
  47. #        11   domain of last voter 30 bytes
  48. #
  49.  
  50. # If a vote is being registered
  51.  
  52. if ($dict{'movie'}        &&
  53.     $dict{'movie'} !~ /</ &&
  54.     $dict{'score'} >= 1   &&
  55.     $dict{'score'} <= 10)
  56. {
  57.     tie %movies, SDBM_File, 'movies', O_CREAT|O_RDWR, 0660;
  58.  
  59.     print "Your vote of $dict{score} out of 10 for ",
  60.           "<B>$dict{movie}</B> has been recorded.<HR>\n";
  61.  
  62.     $movie   = $dict{'movie'};
  63.     @rec     = unpack("l11a30",$movies{$movie});
  64.     $rec[11] = $ENV{'REMOTE_HOST'};
  65.     $rec[0]  = $dict{'score'};
  66.     $rec[$dict{'score'}]++;
  67.     $movies{$movie} = pack("l11a30",@rec);
  68.     
  69.     untie %movies;
  70. }
  71.  
  72. tie %movies, SDBM_File, 'movies', O_CREAT|O_RDONLY, 0660;
  73.  
  74. foreach $title (sort keys(%movies))
  75. {
  76.     # unpack the record into an array
  77.     @rec = unpack("l11a30",$movies{$title});
  78.  
  79.     # calculate the average and total number of votes
  80.     ($avg, $votes) = calcTotals(@rec[1..10]);
  81.  
  82.     print "<H2>$title</H2><BLOCKQUOTE>";
  83.     print '<B>Average: ';
  84.     printf('%2.2f',$avg);
  85.     print " Votes: $votes</B><BR>\n";
  86.     print '<IMG SRC="ht72a.pl?score=', join('+',@rec[1..10]),
  87.           qq{" ALT="};
  88.     foreach (1..10)
  89.     {
  90.         print "$_:$rec[$_] ";
  91.     }
  92.     print qq{"><BR>\n};
  93.     print "Last vote from <B>$rec[11]</B> ";
  94.     print "who gave $title a $rec[0] out of 10.\n";
  95.  
  96.     # Build the Form for voting for this movie
  97.     print qq{<FORM ACTION="$ENV{'SCRIPT_NAME'}">};
  98.     print qq{<INPUT TYPE="hidden" NAME="movie" value="$title">}; 
  99.     print qq{<SELECT NAME="score">};
  100.     # output <OPTION> 1 <OPTION> 2 ... <OPTION> 10
  101.     print join '<OPTION>', ' ', 1..10;
  102.     print '</SELECT><INPUT TYPE="submit" value="Vote"></FORM>';
  103.     print "</BLOCKQUOTE><BR>\n";
  104. }
  105.  
  106. # Build the Form for voting for 'Other'
  107. print "<H2>Other</H2><BLOCKQUOTE>";
  108. print qq{<FORM ACTION="$ENV{'SCRIPT_NAME'}">};
  109. print qq{<INPUT NAME="movie">}; 
  110. print qq{<SELECT NAME="score">};
  111. print join '<OPTION>', ' ', 1..10; 
  112. print '</SELECT><INPUT TYPE="submit" value="Vote"></FORM>';
  113. print "</BLOCKQUOTE>";
  114.  
  115. print '<HR></BODY></HTML>';
  116. 1;
  117.  
  118.